﻿namespace Dcms.Common
{
    using System;
    using System.Web;

    public class Jscript
    {
        public static void Alert(string message)
        {
            string js = "<Script language='JavaScript'>\r\n                    alert('" + message + "');</Script>";
            HttpContext.Current.Response.Write(js);
        }

        public static void AlertAndBack(string message)
        {
            string js = "<Script language='JavaScript'>\r\n                    alert('" + message + "');history.go(-1);</Script>";
            HttpContext.Current.Response.Write(js);
        }

        public static void AlertAndLogOut(string message, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');top.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        }

        public static void AlertAndRedirect(string message, string toURL)
        {
            string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
            HttpContext.Current.Response.Write(string.Format(js, message, toURL));
        }

        public static void CloseWindow()
        {
            string js = "<Script language='JavaScript'>\r\n                    parent.opener=null;window.close();  \r\n                  </Script>";
            HttpContext.Current.Response.Write(js);
            HttpContext.Current.Response.End();
        }

        public static void GoHistory(int value)
        {
            string js = "<Script language='JavaScript'>\r\n                    history.go({0});  \r\n                  </Script>";
            HttpContext.Current.Response.Write(string.Format(js, value));
        }

        public static void JavaScriptLocationHref(string url)
        {
            string js = "<Script language='JavaScript'>\r\n                    window.location.replace('{0}');\r\n                  </Script>";
            js = string.Format(js, url);
            HttpContext.Current.Response.Write(js);
        }

        public static void JsRedirect(string MyUrl)
        {
            string js = "<Script language='JavaScript'>\r\n                    function dolink(){location.href='" + MyUrl + "'}setTimeout('dolink()',1000);</script>";
            HttpContext.Current.Response.Write(js);
        }

        public static void OpenWebFormSize(string url, int width, int heigth, int top, int left)
        {
            string js = string.Concat(new object[] { "<Script language='JavaScript'>window.open('", url, "','','height=", heigth, ",width=", width, ",top=", top, ",left=", left, ",location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');</Script>" });
            HttpContext.Current.Response.Write(js);
        }

        public static void RefreshOpener()
        {
            string js = "<Script language='JavaScript'>\r\n                    opener.location.reload();\r\n                  </Script>";
            HttpContext.Current.Response.Write(js);
        }

        public static void RefreshParent(string url)
        {
            string js = "<Script language='JavaScript'>\r\n                    window.opener.location.href='" + url + "';window.close();</Script>";
            HttpContext.Current.Response.Write(js);
        }

        public static string ShowModalDialogJavascript(string webFormUrl, string features)
        {
            return ("<script language=javascript>\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\tshowModalDialog('" + webFormUrl + "','','" + features + "');</script>");
        }

        public static void ShowModalDialogWindow(string webFormUrl, string features)
        {
            string js = ShowModalDialogJavascript(webFormUrl, features);
            HttpContext.Current.Response.Write(js);
        }

        public static void ShowModalDialogWindow(string webFormUrl, int width, int height, int top, int left)
        {
            string features = "dialogWidth:" + width.ToString() + "px;dialogHeight:" + height.ToString() + "px;dialogLeft:" + left.ToString() + "px;dialogTop:" + top.ToString() + "px;center:yes;help=no;resizable:no;status:no;scroll=yes";
            ShowModalDialogWindow(webFormUrl, features);
        }
    }
}

